﻿(function () {
    'use strict';
    var controllerId = 'dashboard';
    angular.module('app').controller(controllerId, ['common', 'datacontext', dashboard]);

    function dashboard(common, datacontext) {
        var getLogFn = common.logger.getLogFn;
        var log = getLogFn(controllerId);

        var vm = this;
        vm.news = {
            title: 'Hot Towel Angular',
            description: 'Hot Towel Angular is a SPA template for Angular developers.'
        };
        vm.messageCount = 0;
        vm.people = [];
        vm.SPContacts = [];
        vm.Customers = [];
        vm.title = 'Dashboard';
        vm.busyMessage = 'Please wait ...';
        vm.isBusy = true;
        vm.spinnerOptions = {
            radius: 40,
            lines: 7,
            length: 0,
            width: 30,
            speed: 1.7,
            corners: 1.0,
            trail: 100,
            color: '#F58A00'
        };

        activate();

        function activate() {
            var promises = [getMessageCount(), getPeople(), getSPContacts(), getCustomers()];
            common.activateController(promises, controllerId)
                .then(function () { log('Activated Dashboard View'); });
        }

        function getCustomers() {
            toggleSpinner(true);
            return datacontext.getCustomers().then(function (data) {
                toggleSpinner(false);
                return vm.Customers = data;
                
            });
        }

        function getMessageCount() {
            return datacontext.getMessageCount().then(function (data) {
                return vm.messageCount = data;
            });
        }

        function getPeople() {
            return datacontext.getPeople().then(function (data) {
                return vm.people = data;
            });

        }

        function getSPContacts() {

            return datacontext.getSPContacts().then(function (data) {
                return vm.SPContacts = data;
            });

        }

        function toggleSpinner(on) { vm.isBusy = on; }
    }
})();